package com.sjh.controller;

import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sjh.service.ISjh_service;
import com.sjh.vo.Bis_nurse_project;
import com.sjh.vo.Bis_nurse_type;





@Controller
public class Sjh_controller {
		@Autowired
		private ISjh_service service;
		
		int a=30000023;
		//找项目
		@RequestMapping("selectproject")
		public String selectproject(Map<String, Object> map,Bis_nurse_project project){
			List<Map> list = service.selectproject(project);
			map.put("list", list);
			//分类模糊查询
			List<Bis_nurse_type> list2 = service.addtype();
			map.put("list2", list2);
			return "sjh/select";
		}
		
		@RequestMapping("delect/{project_id}")
		public String delect(@PathVariable String project_id){
			service.delect(project_id);
			
			return "redirect:/selectproject";
		}
		@RequestMapping("addproject")
		public String addproject(Bis_nurse_project project,@RequestParam("file")MultipartFile[] file,HttpServletRequest request){
			a++;
			project.setProject_id(a+"");
			
			String path=request.getRealPath("/upload");//tomcat/project
			File file1=new File(path);
			path=file1.getParent();//webapp
			path=path+"/upload/";
			Date date=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
			//path:c:/tools/tomcat/webapp/upload/20180719
			path=path+sdf.format(date);
			File dir=new File(file1.getPath(),sdf.format(date));
			if(!dir.exists()){
				//如果该目录不存在则创建目录
				dir.mkdirs();
			}
			File cover_photo=new File(dir.getPath(),file[0].getOriginalFilename());
			File cover_big_photo=new File(dir.getPath(),file[1].getOriginalFilename());
			try{
				if(!cover_photo.exists()){
					//此时创建之后只是一个空的文件
					cover_photo.createNewFile();
				}
				//将file中的内容复制到newfile中
				file[0].transferTo(cover_photo);
				file[1].transferTo(cover_big_photo);
			}catch(Exception e){
				e.printStackTrace();
			}
			
			project.setCover_photo("/upload/"+dir.getName()+"/"+cover_photo.getName());
			project.setCover_big_photo("/upload/"+dir.getName()+"/"+cover_big_photo.getName());
			
			
			service.addproject(project);
			return "redirect:/selectproject";
		}
		@RequestMapping("addtype")
		public String addtype(Model model){
			List<Bis_nurse_type> list = service.addtype();
			model.addAttribute("list", list);
			return "sjh/insert";
		}
		@RequestMapping("updateByid/{project_id}")
		public String updateByid(@PathVariable String project_id,Model model){
			Bis_nurse_project project = service.updateByid(project_id);
			/*System.out.println("project："+project);*/
			model.addAttribute("project",project);
			
			List<Bis_nurse_type> list = service.addtype();
			model.addAttribute("listb", list);
			/*System.out.println(list.get(0).getType_name());*/
			
			return "sjh/update";
		}
		@RequestMapping("update")
		public String update(Bis_nurse_project project,@RequestParam("file")MultipartFile[] file,HttpServletRequest request){
			
			String path=request.getRealPath("/upload");//tomcat/project
			File file1=new File(path);
			/*path=file1.getParent();//webapp
			path=path+"/upload/";*/
			Date date=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
			if(file[0].getOriginalFilename()==null||file[0].getOriginalFilename()=="" || file[1].getOriginalFilename()==null||file[1].getOriginalFilename()=="" ){
				project.setCover_photo(request.getParameter("photoname1"));
				project.setCover_big_photo(request.getParameter("photoname2")); 
			   }
			
			else{
				 //path:c:/tools/tomcat/webapp/upload/20180719
					/*path=path+sdf.format(date);*/
					File dir=new File(file1.getPath(),sdf.format(date));
					if(!dir.exists()){
						//如果该目录不存在则创建目录
						dir.mkdirs();
					}
					File cover_photo=new File(dir.getPath(),file[0].getOriginalFilename());
					File cover_big_photo=new File(dir.getPath(),file[1].getOriginalFilename());
					try{
						if(!cover_photo.exists()){
							//此时创建之后只是一个空的文件
							cover_photo.createNewFile();
						}
						//将file中的内容复制到newfile中
						file[0].transferTo(cover_photo);
						file[1].transferTo(cover_big_photo);
					}catch(Exception e){
						e.printStackTrace();
					}
					
					project.setCover_photo("/upload/"+dir.getName()+"/"+cover_photo.getName());
					project.setCover_big_photo("/upload/"+dir.getName()+"/"+cover_big_photo.getName());
					 
				   
			   }
			
			
			service.update(project);
			return "redirect:/selectproject";
		}
		  
		
		
		//详情
		@RequestMapping(value="selectByid",produces = "text/plain;charset=utf-8")
		@ResponseBody
		public void selectByid(String project_id,HttpServletResponse response){
			String jsonStr;
			try {
				jsonStr = new ObjectMapper().writeValueAsString(service.selectByid(project_id));
				response.getWriter().write(jsonStr);
			} catch (Exception e) {
			}
		}
	
}
